package com.steven.team.dao

import com.steven.team.pojo.MatchCollection
import com.steven.team.pojo.MatchInfo
import com.steven.team.pojo.TeamCollection
import com.steven.team.pojo.TeamInfo
import org.apache.ibatis.annotations.*
import org.springframework.stereotype.Repository

@Repository
interface MatchDao {

    @Select("<script>"
            + "select match_info.*,count(matchId) sum from match_info left join match_history mh on match_info.id = mh.matchId where match_info.id in "
            + "<foreach item='item' index='index' collection='str' open='(' separator=',' close=')'>"
            + "#{item}"
            + "</foreach> group by match_info.id order by sum desc"
            + "</script>")
    fun searchMatchDetail(str: List<String>): List<MatchInfo>

    @Select("select * from match_info where id = #{id}")
    fun searchMatchDetailById(id: Long): List<MatchInfo>

    @Insert("insert into match_history(uid, matchId, preference,createTime) VALUE (#{uid},#{matchId},3,#{createTime})\n" +
            " on duplicate key update preference=preference+0.3,createTime = #{createTime}")
    fun addHistory(uid: Long, matchId: Long, createTime: Long = System.currentTimeMillis()): Int

    @Select("select team.*, count(team_member.id) memberNum\n" +
            "from (select team_info.id,\n" +
            "             team_info.matchId,\n" +
            "             mi.name    matchName,\n" +
            "             team_info.teamName,\n" +
            "             belongUserId,\n" +
            "             team_info.createTime,\n" +
            "             team_info.status,\n" +
            "             ui.uname   leaderName,\n" +
            "             ui.iconUrl leaderIconUrl\n" +
            "      from team_info\n" +
            "               left join match_info mi on team_info.matchId = mi.id\n" +
            "               left join user_detail ui on team_info.belongUserId = ui.uid\n" +
            "      where matchId = #{matchId} and team_info.status=1) as team\n" +
            "         left join team_member on team_member.teamId = team.id\n" +
            "group by team.id\n" +
            "order by team.createTime desc")
    fun searchTeamByMatch(matchId: Long): List<TeamInfo>

    @Select("select mi.*, count(uid) sum\n" +
            "from match_history\n" +
            "         left join match_info mi on match_history.matchId = mi.id\n" +
            "where match_history.createTime > #{time}\n" +
            "group by mi.id\n" +
            "order by sum desc, convert(name using gbk), convert(mi.belong using gbk) limit 10")
    fun searchTop10MatchRecent(time: Long): List<MatchInfo>

    @Select("select mi.*\n" +
            "from match_history\n" +
            "         left join match_info mi on match_history.matchId = mi.id\n" +
            "where uid = #{uid}\n" +
            "order by createTime desc")
    fun searchHistory(uid: Long): List<MatchInfo>

    @Insert("insert into match_collection(matchId, uid, createTime) VALUE (#{matchId},#{uid},#{createTime})\n" +
            "on duplicate key update createTime=#{createTime}")
    fun addToCollection(matchId: Long, uid: Long, createTime: Long = System.currentTimeMillis()): Int

    @Delete("delete from match_collection where matchId=#{matchId} and uid=#{uid}")
    fun deleteCollectionById(uid: Long, matchId: Long): Int

    @Select("select match_info.id,\n" +
            "       name,\n" +
            "       belong,\n" +
            "       rank,\n" +
            "       signUpStartTime,\n" +
            "       signUpEndTime,\n" +
            "       matchStartTime,\n" +
            "       matchEndTime,\n" +
            "       url,\n" +
            "       createTime\n" +
            "from match_collection\n" +
            "         left join match_info on matchId = match_info.id\n" +
            "where uid = #{uid} and createTime<#{lastCreateTime}\n" +
            "order by createTime desc\n" +
            "limit 20")
    fun selectCollectionsByUser(uid: Long, lastCreateTime: Long): List<MatchCollection>

    @Select("select match_info.id,\n" +
            "       name,\n" +
            "       belong,\n" +
            "       `rank`,\n" +
            "       signUpStartTime,\n" +
            "       signUpEndTime,\n" +
            "       matchStartTime,\n" +
            "       matchEndTime,\n" +
            "       url,\n" +
            "       createTime\n" +
            "from match_collection\n" +
            "         left join match_info on matchId = match_info.id\n" +
            "where uid = #{uid} and matchId=#{matchId}")
    fun selectCollectionsByMatchId(uid: Long, matchId: Long): List<MatchCollection>

    @Insert("insert into match_info(name, belong, `rank`, signUpStartTime, signUpEndTime, matchStartTime, matchEndTime, url)\n" +
            "VALUES (#{name}, #{belong} ,#{rank}, #{signUpStartTime}, #{signUpEndTime}, #{matchStartTime}, #{matchEndTime}, #{url})")
    fun addMatch(matchInfo: MatchInfo): Int

    @Select("select * from match_info where name=#{name} and belong=#{belong}")
    fun searchMatchByNameAndBelong(name: String, belong: String): List<MatchInfo>
}